package com.tj.tj_java.service.impl;

import com.tj.tj_java.mapper.FavoriteMapper;
import com.tj.tj_java.mapper.MusicMapper;
import com.tj.tj_java.pojo.Music;
import com.tj.tj_java.result.Result;
import com.tj.tj_java.service.MusicService;
import com.tj.tj_java.vo.MusicVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class MusicServiceImpl implements MusicService {

    @Autowired
    private MusicMapper musicmapper;  //音乐mapper

    @Autowired
    private FavoriteMapper favoriteMapper;

    //根据歌曲id获取该歌曲
    @Override
    public Result getMusicById(int musicId) {
        return Result.success("成功获取该id音乐",musicmapper.getMusicById(musicId));
    }
    //首页获取全部歌曲，显示用户收藏模糊查询
    @Override
    public Result homeMusicList(int userId,String musicTitle) {
        List<Music> Musics = musicmapper.getMusicByTitle(musicTitle);//获取全部歌曲
        List<Music> userMusics = favoriteMapper.getUserMusicById(userId);//获取用户收藏
        //创建要返回给前端的收藏数组
        List<MusicVo> resultList = new ArrayList<>();
        //遍历该全部歌曲给每歌曲添加star为false的状态
        for (Music music : Musics) {
            MusicVo musicVo = MusicVo.builder()
                    .musicId(music.getMusicId())
                    .musicTitle(music.getMusicTitle())
                    .imgAddress(music.getImgAddress())
                    .singer(music.getSinger())
                    .Star(false)
                    .build();
            resultList.add(musicVo);
            //遍历用户收藏歌单
            for (Music userMusic :userMusics){
                //如果是用户收藏的歌曲则改变star为true
                if (musicVo.getMusicId()==userMusic.getMusicId()){
                    musicVo.setStar(true);}
            }
        }
        return Result.success("成功",resultList);
    }
}
